package cn.clq.org.service.impl;

import cn.clq.basic.exception.BusinessException;
import cn.clq.basic.service.impl.BaseServiceImpl;
import cn.clq.basic.util.BaiduAuditUtils;
import cn.clq.basic.util.MailUtils;
import cn.clq.basic.constant.Constant;
import cn.clq.org.domain.Employee;
import cn.clq.org.domain.Shop;
import cn.clq.org.domain.ShopAuditLog;
import cn.clq.org.mapper.EmployeeMapper;
import cn.clq.org.mapper.ShopAuditLogMapper;
import cn.clq.org.mapper.ShopMapper;
import cn.clq.org.service.IShopService;
import cn.clq.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;


    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * @description: 商家入驻的业务逻辑实现
     * @param: shop
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/12 12:46
     */
    @Override
    public void settlement(Shop shop) {
        //1.空内容校验
        Employee admin = shop.getAdmin();
        if(StringUtils.isEmpty(shop.getName())
            || StringUtils.isEmpty(shop.getAddress())
            || StringUtils.isEmpty(admin.getUsername())
            || StringUtils.isEmpty(admin.getPhone())){
            throw new BusinessException("请填写完整的入驻信息");
        }
        //2.合法性校验，利用AI自动审核
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("你填写的信息含有敏感字符，请重新填写");
        }
        //3.判断店铺和店长是否已经注册
        //3.1 如果店铺已经被注册了，直接报错
        if(shopMapper.loadByName(shop.getName()) != null){
            throw new BusinessException("同名店铺已经存在，请更换店名");
        }
        //3.2 判断店长是否已经注册
        //注册过，没店铺
        Employee empTmp = employeeMapper.loadByPhone(admin.getPhone());
        if( empTmp != null && empTmp.getShop_id() != null){
            //注册过，有店铺   抛出异常
            throw new BusinessException("该管理员名下已有店铺，请更换管理员");
        }
        //没有注册过
        if(empTmp == null){
            //没注册过
            employeeMapper.save(admin);//这里返回了自增id
            empTmp=admin;
        }

        //注册过，没店铺,直接保存数据

        //校验logo是否合法
        if(!BaiduAuditUtils.ImgCensor(Constant.FastDfs.FASTDFS+shop.getLogo())){
            throw new BusinessException("该logo不符合规定，请更换");
        }
        //4.保存数据
        //到了这一步之后，empTmp是一定有值的，并且这里不能用admin，因为admin是我们输入的数据,没有id，
        //如果时注册过没店铺，那么empTmp本来就有值，如果时新员工，保存之后也返回了自增id
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);//这里返回了自增id

        //这里无法修改员工的信息，因为是用数据表查出来的empTmp来操作的，并没有将输入的admin的值传入
        empTmp.setShop_id(shop.getId());
        employeeMapper.updateById(empTmp);
    }


    /**
     * @description: 店铺审核通过
     * @param: shopAuditLog
     * @return: void
     * @author clqlongbao
     * @date: 2022/6/13 18:29
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态
        Long shopId = shopAuditLog.getShopId();//被审核店铺的id
        Shop shop = shopMapper.loadById(shopId);//找到被审核的店铺的信息
        shop.setState(Constant.ShopState.ACTIVATING);//设置状态为通过审核待激活
        shopMapper.updateById(shop);//保存修改信息

        //2.记录日志
        shopAuditLog.setState(Constant.ShopState.ACTIVATING);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(shop.getAdminId());
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送激活邮件
//        SimpleMailMessage mailMessage = new SimpleMailMessage();
//        mailMessage.setFrom("1272352079@qq.com");//设置发送人
//        mailMessage.setSubject("宠物乐园-店铺入驻激活邮件");//邮件主题
//        mailMessage.setText("亲，您的店铺入驻信息已经通过审核，请尽快完成激活并使用：" +
//                "激活链接：http://localhost:8080/shop/activation/"+shopAuditLog.getShopId());//邮件内容
//        mailMessage.setTo("1272352079@qq.com");//收件人
//        javaMailSender.send(mailMessage);
        MailUtils.sendSimpleMail(javaMailSender,Constant.Mail.MYMAIL,shop);
    }


    /**
     * @description: 激活店铺
     * @param: id
     * @return: void
     * @author clqlongbao
     * @date: 2022/6/13 20:11
     */
    @Override
    public String activation(Long id) {
        //得到用户想要激活的店铺信息
        Shop shop = shopMapper.loadById(id);
        if(shop==null || shop.getState()<1){
            //说明用户擅自改动了链接
            return "无效链接";
        }
        if(shop.getState()==Constant.ShopState.ACTIVATED){
            return "请勿重复激活";
        }
        shop.setState(Constant.ShopState.ACTIVATED);
        shopMapper.updateById(shop);
        //激活成功还需要在日志表里面新增一条日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(shop.getId());
        shopAuditLog.setNote("激活成功");
        shopAuditLog.setState(Constant.ShopState.ACTIVATED);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(shop.getAdminId());
        shopAuditLogMapper.save(shopAuditLog);
        return "激活成功";
    }




    /**
     * @description: 店铺审核被驳回
     * @param: shopAuditLog
     * @return: void
     * @author clqlongbao
     * @date: 2022/6/13 18:30
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态
        Long shopId = shopAuditLog.getShopId();//被审核店铺的id
        Shop shop = shopMapper.loadById(shopId);//找到被审核的店铺的信息
        shop.setState(Constant.ShopState.REJECT);//设置状态为驳回
        shopMapper.updateById(shop);//保存修改信息

        //2.记录日志
        shopAuditLog.setState(Constant.ShopState.REJECT);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(shop.getAdminId());
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送通知邮件，目前通过链接修改信息功能没实现，因为拿做不到数据回显，只能是重新走新增流程
//        SimpleMailMessage mailMessage = new SimpleMailMessage();
//        mailMessage.setFrom("1272352079@qq.com");//设置发送人
//        mailMessage.setSubject("宠物乐园-店铺入驻驳回邮件");//邮件主题
//        mailMessage.setText("亲，您的店铺入驻信息存在违规信息，请复制链接到新页面打开" +
//                "http://localhost:8081/#/shopRegister");//邮件内容
//        mailMessage.setTo("1272352079@qq.com");//收件人
//        javaMailSender.send(mailMessage);
        MailUtils.sendSimpleMail(javaMailSender,Constant.Mail.MYMAIL,shop);

    }

    /**
     * @description: 批量新增
     * @param: list
     * @return: void
     * @author clqlongbao
     * @date: 2022/6/14 14:59
     */
    @Override
    public void batchSave(List<Shop> list) {
        shopMapper.batchSave(list);
    }
    /**
     * @description: 查询echarts需要的横纵坐标
     * @param:
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @author clqlongbao
     * @date: 2022/6/14 18:24
     */
    @Override
    public Map<String,Object> queryEcharts() {
        List<ShopVo> shopVos = shopMapper.queryEcharts();
        List<String> states = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> countNum = shopVos.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",countNum);
        return map;
    }


}
